package com.example.vv.frameWork.code;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.vv.frameWork.code.ConfigCode;
import com.example.vv.frameWork.code.ReqInterface;
import com.example.vv.frameWork.entity.LoginEntity;
import com.example.vv.frameWork.sp.Userinfo;
import com.example.vv.frameWork.until.SharePUntitl;
import com.example.vv.req.BaseRequest;
import com.example.vv.res.BaseRespont;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ReqUntile {


    private static ReqInterface reqInterface;

    static {
        reqInterface = new Retrofit
                .Builder()
                .baseUrl(ConfigCode.IP)
                .client(new OkHttpClient.Builder().build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ReqInterface.class);
    }

    public static Observable<BaseRespont> request(String API, BaseRequest baseRequest){
        baseRequest.initMap();
        Userinfo userinfo = SharePUntitl.userinfo;
        if(userinfo!=null){
            baseRequest.map.put("userId", SharePUntitl.userinfo.getPhone());
        }
        return reqInterface.POST_API(baseRequest.map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Function<ResponseBody, BaseRespont>() {
             @Override
             public BaseRespont apply(ResponseBody responseBody)  {

                 JSONObject jb = null;
                 try {
                     jb = JSON.parseObject(responseBody.string());
                     String success = jb.getString("success");
                     String message = jb.getString("message");
                     String code = jb.getString("code");
                     String baseData = jb.getString("baseData");
                     BaseRespont respont = new BaseRespont();
                     respont.setBaseData(baseData);
                     respont.setMessage(message);
                     respont.setCode(code);
                     respont.setSuccess(success);
                     return respont;
                 } catch (IOException e) {
                     e.printStackTrace();
                     return null;
                 }


             }
         });
    }

}
